package com.example.demo.controller;

import com.example.demo.qniuutil.QiniuUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("Image")
public class imageController {

    @RequestMapping("upload")
    @ResponseBody
    public HashMap<String, Object> upload(@RequestParam("file") MultipartFile imgFile){
        HashMap<String,Object> result = new HashMap<String,Object>();
        String originalFilename = imgFile.getOriginalFilename();//获取图片原始文件名
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index);//获得图片后缀名  .jpg
        String fileName = UUID.randomUUID().toString() + extention; //进行拼接
        fileName = fileName.replace("-","");
        try {
            //使用工具类将文件上传到七牛云服务器
            String filePath = QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            result.put("filePath",filePath);
        } catch (IOException e) {

        }
        return result;
    }
}
